package com.mbc.web.common.server.util;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileInputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.InputStreamReader;
import java.util.ArrayList;

public class FileUtil {

	public static ArrayList<String> readFile(String filePath) throws Exception
	{
		ArrayList<String> l_result = new ArrayList<String>();
		
		try {
			BufferedReader l_BufferedReader = new BufferedReader(new FileReader(filePath));
			String l_str = "";
			while ((l_str = l_BufferedReader.readLine()) != null) {
				l_result.add(l_str);
			}
			l_BufferedReader.close();
			
		} catch (Exception e) {
			throw e;
		}
		
		return l_result;
	}
	
	public static ArrayList<String> readFile(String filePath, String encoding) throws Exception
	{
		ArrayList<String> l_result = new ArrayList<String>();
		
		try {
			FileInputStream l_fs = new FileInputStream(filePath);
			BufferedReader l_BufferedReader = new BufferedReader(new InputStreamReader(l_fs, encoding));
			String l_str = "";
			while ((l_str = l_BufferedReader.readLine()) != null) {
				l_result.add(l_str);
			}
			l_BufferedReader.close();
			
		} catch (Exception e) {
			throw e;
		}
		
		return l_result;
	}
	
	public static boolean writeFile(ArrayList<String> stringList, String filePath) throws Exception
	{
		try {
			
			FileWriter l_FilWriter = new FileWriter(filePath, false);
			BufferedWriter l_BufferWriter = new BufferedWriter(l_FilWriter);
			for (String string : stringList) {
				l_BufferWriter.write(string);
				l_BufferWriter.newLine();
			}
			l_BufferWriter.close();
		} catch (Exception e) {
			throw e;
		}
		return true;
	}
}
